#!/usr/bin/python

import sys
from Bio import SeqIO

def trans(table):
    def mkrecs(handle):
        for r in SeqIO.parse(handle, 'fasta'):
            if  r.id in table:
                yield SeqIO.SeqRecord(r.seq, table[r.id], '', '')
            else: 
                yield r
    
    SeqIO.write(mkrecs(sys.stdin), sys.stdout, 'fasta')

if __name__ == '__main__':
    import sys, getopt
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'f:r')
    except:
        raise
    else:
        dopts = dict(opts)
    
    table = {}
    for l in file(dopts['-f']):
        if dopts.get('-r', False):
            value, key = l.split()
        else:
            key, value = l.split()
        table[key] = value
    trans(table)
